package com.fs.jzzn.service;

import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fs.base.utils.ApplicationUtils;
import com.fs.jzzn.pojo.Flow;
import com.fs.jzzn.pojo.FlowStatus;
import com.fs.jzzn.pojo.Information;
import com.fs.jzzn.pojo.Project;
import com.fs.jzzn.repository.InformationRepository;
import com.fs.jzzn.repository.ProjectRepository;
@Service("projectService")
@Transactional
public class ProjectService {
	private static final Logger log = Logger.getLogger(ProjectService.class);
	@Autowired ProjectRepository projectRepository;
	@Autowired InformationRepository informationRepository;
	@Autowired FlowService flowService;
	
	/**
	 * 保存立项记录，同时如果已经绑定流程，自动生成下一个流程节点
	 * @param project
	 */
	@Transactional
	public void save(Project project){
		try {
			projectRepository.save(project);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
	/**
	 * @param 流程执行
	 */
	@Transactional
	public void action(Map<String, Object> data,String projectId){
		Project p = projectRepository.findOne(projectId);
		if(p!=null){
			FlowStatus instance = flowService.action(p.getFlowId(), projectId, p.getClass().getName());
			Flow nextNode = flowService.getNextNode(instance.getFlowId(), instance.getNodeId());
			if(nextNode==null) return ;
			
			Flow currentNode = flowService.getFlowRepository().findOne(instance.getNodeId());
			/**
			 * 流程业务处理 
			 */
			if(currentNode!=null){
				String flowCode = currentNode.getFlowCode();
				if("A".equals(flowCode)){
					String json = JSON.toJSONString(data);
					Project infor = JSON.parseObject(json, Project.class);
					projectRepository.save(infor);
					//新建
				}else if("B".equals(flowCode)){
					//施工表单信息填写
					String json = JSON.toJSONString(data);
					Information infor = JSON.parseObject(json, Information.class);
					informationRepository.save(infor);
				}else if("C".equals(flowCode)){
					//完工验收
					String json = JSON.toJSONString(data);
					Information infor = JSON.parseObject(json, Information.class);
					Information inforNew = informationRepository.findOne(infor.getId());
					inforNew.setOpticCableCores(infor.getOpticCableCores());
					inforNew.setOpticCableLength(infor.getOpticCableLength());
					inforNew.setTogether(infor.getTogether());
					inforNew.setZtLen(infor.getZtLen());
					inforNew.setDlLen(infor.getDlLen());
					inforNew.setRemark(infor.getRemark());
					inforNew.setAttachment(infor.getAttachment());
					informationRepository.save(inforNew);
				}else if("D".equals(flowCode)){
					//资管交维
					String json = JSON.toJSONString(data);
					Information infor = JSON.parseObject(json, Information.class);
					Information inforNew = informationRepository.findOne(infor.getId());
					String att = inforNew.getAttachment();
					if(StringUtils.isNotBlank(infor.getAttachment())){
						att = ","+ infor.getAttachment();
						inforNew.setAttachment(att);
						informationRepository.save(inforNew);
					}
				}else if("E".equals(flowCode) || "D1".equals(flowCode)){
					//竣工资料/送审
					String json = JSON.toJSONString(data);
					Information infor = JSON.parseObject(json, Information.class);
					Information inforNew = informationRepository.findOne(infor.getId());
					String att = inforNew.getAttachment();
					if(StringUtils.isNotBlank(infor.getAttachment())){
						att = ","+ infor.getAttachment();
						inforNew.setAttachment(att);
						informationRepository.save(inforNew);
					}
				}else if("F".equals(flowCode)){
					//审计
					String json = JSON.toJSONString(data);
					Project project = JSON.parseObject(json, Project.class);
					p.setActualMoney(project.getActualMoney());
					p.setAttachment(project.getAttachment());
				}else if("G".equals(flowCode)){
					//完结
				}else{
					System.out.println("状态未定义");
				}
			}
			/*
			 * 更新项目状态
			 */
			p.setStatus(nextNode.getFlowName());
			projectRepository.save(p);
			/*
			 * 更新到下一个节点
			 */
			flowService.nextNode(instance);
		}
	}

	/**
	 * 回退
	 * @param projectId
	 */
	@Transactional
	public void back(String projectId) {
		//更新当前流程节点
		Project project = projectRepository.findOne(projectId);
		Flow currentNode = flowService.getCurrentNode(project.getFlowId(), projectId);
		Flow preNode = flowService.getPreNode(project.getFlowId(), currentNode.getId());
		if(preNode!=null){
			project.setStatus(preNode.getFlowName());
			projectRepository.save(project);
		}
		FlowStatus instance = flowService.getFlowStatusRepository().findByFlowIdAndModleId(project.getFlowId(), project.getId());
		if(instance!=null){
			//流程状态更新
			flowService.recordProcess(instance,ApplicationUtils.userId(), instance.getNodeName().concat("【驳回处理】！"));
		}
		flowService.preNode(project, preNode);
	}
}
